/**
 * 
 */
package com.alstudio.ui.module.main;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.pojo.module.user.User;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.android.keyboard.ALKeyBoardManager;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.absadapter.BaseUserListAdapter;
import com.alstudio.view.edittext.ALEditText;
import com.alstudio.view.edittext.ALEditText.OnALEditorActionListener;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView.RankViewLoadStateListener;
import com.alstudio.view.toast.MyToastUtil;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class SearchUserActivity extends TitleBarActivity implements
		OnALEditorActionListener, RankViewLoadStateListener {

	private PullRefreshAndLoadMoreView search_listview;
	// titlebar
	private View titlebar;
	// 输入框
	private ALEditText inputEditText;
	// 取消按钮
	private View btnCancel;
	// 搜索结果
	private ArrayList<User> searchResult;
	// 数据适配器
	private BaseUserListAdapter userAdapter;
	// id、关键字搜索
	private final String SEARCH_BY_UID = "uid";
	// 城市搜索
	private final String SEARCH_BY_CITY = "city";
	//好友搜索
	private final String SEARCH_FAVORITES = "favorites";
	// 搜索方式
	private String search_type = SEARCH_BY_UID;
	// 搜索关键字
	private String keyword;
	// 是否重新搜索
	private boolean isKeyWordReset = true;

	private boolean isSearchFriend = false;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#setupView(android.content.Context,
	 * java.lang.String)
	 */
	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.search_user_layout);
		search_listview = (PullRefreshAndLoadMoreView) findViewById(R.id.search_listview);
		// 禁止下拉刷新
		search_listview.disablePullRefresh();
		// 获取搜索栏
		titlebar = getLayoutInflater().inflate(
				R.layout.search_layout_titlebar_layout, null);
		inputEditText = (ALEditText) titlebar.findViewById(R.id.searh_input);
		btnCancel = titlebar.findViewById(R.id.cancel_bt);
		btnCancel.setOnClickListener(this);
		// 改变返回键为搜索按钮
		inputEditText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		inputEditText.setALEditorActionListener(this);
		setCustomTitleBar(titlebar);
		initData();
		if (!isSearchFriend) {
			inputEditText.setHint(R.string.TxtIDSearch);
		}else{
			search_type = SEARCH_FAVORITES;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#initData()
	 */
	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		searchResult = new ArrayList<User>();
		userAdapter = new BaseUserListAdapter(searchResult);
		search_listview.setAdapter(userAdapter);
		search_listview.setLoadStateListener(this);
		isSearchFriend = getIntent().getBooleanExtra("isSearchFriend", false);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.TitleBarActivity#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		ALKeyBoardManager.dismissKeyBoard(this);
		switch (v.getId()) {
		case R.id.cancel_bt:
			finish();
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onEditorAction
	 * ()
	 */
	@Override
	public void onEditorAction() {
		// TODO Auto-generated method stub
		ALLog.d("开始执行搜索");
		invokeSearchRequest();
	}

	private void invokeSearchRequest() {
		String key = inputEditText.getText().toString();
		if (TextUtils.isEmpty(key)) {
			// 搜索内容为空，打出提示
			MyToastUtil.getInstance().showCenterToastOnCenter(
					(R.string.TxtSearchContentNullHint));
			return;
		}
		// 隐藏键盘
		ALKeyBoardManager.dismissKeyBoard(this);
		keyword = key;
		isKeyWordReset = true;
		showLoadingProgress();
		ALUiXmppRequestCreator.postALUiXmppGlobalSearchEvent(
				ALLocalEnv.LOAD_MORE_START_INDEX + "",
				ALLocalEnv.LOAD_MORE_END_INDEX + "", search_type, keyword);
		MyToastUtil.getInstance().showCenterToastOnCenter("正在搜索，请稍后...");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.base.BaseActivity#handleGlobalSearchResult(com.alstudio
	 * .pojo.ALXmppEvent)
	 */
	@Override
	public void handleGlobalSearchResult(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleGlobalSearchResult(data);
		dismissLoadingProgress();
		boolean success = false;
		if (data.getResponseCode() == 0) {
			success = true;
		}

		boolean hasMore = data.getBoolean();

		if (success) {
			search_listview.updateLoadMoreState(hasMore);
			@SuppressWarnings("unchecked")
			ArrayList<User> tmps = (ArrayList<User>) data.getData();
			if (tmps.size() > 0) {
				if (isKeyWordReset) {
					searchResult.clear();
				}
				searchResult.addAll(tmps);

				tmps.clear();
				tmps = null;
				search_listview.showRefereshListView();
				userAdapter.notifyDataSetChanged();
			} else {
				// 没有结果，显示无数据界面
				search_listview.showNoDataInfo(R.drawable.friend_default_01,
						getString(R.string.TxtSearchResultNull));
			}
		}
		isKeyWordReset = false;
		search_listview.onLoadMoreCompleted();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.listview.PullRefreshAndLoadMoreView.
	 * RankViewLoadStateListener
	 * #onLoadMore(com.alstudio.view.listview.PullRefreshAndLoadMoreView)
	 */
	@Override
	public void onLoadMore(PullRefreshAndLoadMoreView view) {
		// TODO Auto-generated method stub
		if (ALLocalEnv.isSendXmppDataEnable()) {
			ALUiXmppRequestCreator.postALUiXmppGlobalSearchEvent(
					view.startIndex + "", view.endIndex + "", search_type,
					keyword);
		} else {
			search_listview.onLoadCompleted();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.listview.PullRefreshAndLoadMoreView.
	 * RankViewLoadStateListener
	 * #onRefresh(com.alstudio.view.listview.PullRefreshAndLoadMoreView)
	 */
	@Override
	public void onRefresh(PullRefreshAndLoadMoreView view) {
		// TODO Auto-generated method stub

		if (ALLocalEnv.isSendXmppDataEnable()) {
			ALUiXmppRequestCreator.postALUiXmppGlobalSearchEvent(
					view.startIndex + "", view.endIndex + "", search_type,
					keyword);
		} else {
			search_listview.onLoadCompleted();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#onContentClear
	 * ()
	 */
	@Override
	public void onContentClear() {
		// TODO Auto-generated method stub
		// 输入框内容被清除了
		// isKeyWordReset = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.edittext.ALEditText.OnALEditorActionListener#
	 * onContentChange(java.lang.String)
	 */
	@Override
	public void onContentChange(String content) {
		// TODO Auto-generated method stub

	}

}
